<template>
  <div class="detail-container">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录账户设置" name="first">
          <el-form ref="form" :model="form" :rules="rules" label-width="200px">
            <el-form-item prop="username" label="姓名：">
              <el-input v-model="form.username" style="width:30%" />
            </el-form-item>
            <el-form-item prop="password" label="密码：">
              <el-input
                v-model="form.password"
                type="password"
                style="width:30%"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <Info />
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">
          <job />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
// 导入组件
import job from './components/Job.vue'
import Info from './components/Info.vue'
// 导入获取用户信息接口
import { getMoreInfo } from '@/api/user'
// 导入修改用户接口
import { saveUserDetailById } from '@/api/employees'
export default {
  components: { Info, job },
  data() {
    return {
      activeName: 'first',
      form: {
        username: '',
        password: ''
      },
      userInfo: {},
      rules: {
        username: [
          { required: true, message: '名字不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 9, message: '输入必须为6-9个字符', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    const res = await getMoreInfo({ id: this.$route.params.id })
    this.userInfo = res
    this.form.username = res.username
    // console.log(res)
  },
  methods: {
    /* eslint-disable */

    //   更新事件
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const res = await saveUserDetailById({
            ...this.userInfo,
            ...this.form
          })
          //   console.log(res)
          //   提示用户
          this.$message.success('更新用户成功')
          this.$router.push({ path: '/employees' })
        } else {
          this.$message.error('输入错误，请重新输入')
          return false
        }
      })
    }
  }
}
</script>

<style></style>
